-- shield 护盾机制
-- 先获得的护盾先 去抗伤害

local mt = ac.buff['护盾']

mt.physics = true
mt.keep = true

mt.cover_max = 99
mt.cover_type = 1

mt.max_shield = 0 -- 护盾上限
mt.shield = 0 -- 护盾值
mt.time = -1

function mt:on_add()
    local unit = self.owner 
    self.max_shield = self.shield
    unit:add('护盾',self.shield)
    self.trigger = unit:on "单位-伤害护盾计算" (function (_, _, source, damage)
        local value = damage:get_current_damage()
        if value > 0 then 
            local cost = math.min(self.shield, value)
            self.shield = self.shield - cost 
            self.owner:add('护盾',- cost)
            value = value - cost
            damage:set_current_damage(value)
            if self.shield <= 0 then 
                -- 护盾破碎的时候触发
                self:notify("on_finish")
                self:remove()
            end
            if value <= 0 then 
                return true
            end
        end 
    end)
end

function mt:on_remove()
    local unit = self.owner 
    unit:add('护盾',-self.shield)
    if self.trigger then 
        self.trigger:remove()
    end 
end

function mt:on_cover()
    return true 
end
